import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Pinyin from '#cpt/temp/js/ChinesePY'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// 
const AddDicModal = $Lazy.state(()=>import('#private/pages/add-dic-modal'))
// 
const DependenceProjectTable 	= $Lazy.load(()=>import('./table/dependence-project'))
const OtherProjectTable 		= $Lazy.load(()=>import('./table/other-project'))
const ProjectAliasTable 		= $Lazy.load(()=>import('./table/project-alias'))
const PriceLinkTable 			= $Lazy.load(()=>import('./table/price-link'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			// 项目名称关联拼音简写 20220704 耿琰
			{ label: '项目全称',		name: 'kind_name', 					half:true, required:true, onChange:({value,formData})=>{
				this.formRef.setValue({'pinyin_name':Pinyin.getWordsCode(value)})
			} },
			{ label: '项目代码',		name: 'kind_code', 					half:true, disabled:true },
			{ label: '项目属性',		name: 'kind_type', 					half:true, required:true, type:'select', loading:true, idStr:'code',  },
			{ label: '检测方法',		name: 'check_method', 				half:true, type:'select', loading:true, idStr:'code', btn: { label:'添加', onClick:()=>{
			  	this.dicRef.open('检测方法', 'check_method', 16000)
			}}},
			{ label: '助记码',		name: 'debit_code', 				half:true },
			{ label: '检测系统',		name: 'kind_group', 				half:true, type:'select', loading:true, required:true, idStr:'code' },
			{ label: '标本类型',		name: 'spec_type', 					half:true, type:'select', loading:true, required:true, idStr:'code', btn: { label:'添加', onClick:()=>{
				this.dicRef.open('标本类型', 'spec_type', 2000)	
			}}},
			{ label: '项目简称',		name: 'short_name', 				half:true },
			{ label: '英文全称',		name: 'name_en', 					half:true },
			{ label: '是否外包',		name: 'is_outsourcing', 			half:true,  type:'radio', onValue:async ({ value, ref, formData})=>{
				$fn.disabledRows(formData,'outsourcing_company_code', +value === 0)
				ref.reset(['project_id','outsourcing_company_code'])
				await this.onOut(value)
				this.setState({ formData })
			}},
			{ label: '英文简称',		name: 'name_short_en', 				half:true },
			{ label: '岗位',			name: 'project_id', 				half:true, type:'select', loading:true, required:true },
			{ label: '拼音简写',		name: 'pinyin_name', 				half:true },
			{ label: '外包单位',		name: 'outsourcing_company_code', 	half:true, type:'select', loading:true, nameStr:'label'},
			{ label: '备注',			name: 'memo', 						half:true, type:'textarea' },
			{ label: '排序',			name: 'sort', 						half:true, type:'number' },
			// 取消系统分类必填 2021-9-23 1:55:58 ?F10: PM? --伍智睿
			{ label: '系统分类',		name: 'sys_category',				half:true,	type:'treeSelect',	checkable:false,	multiple:false,	loading:true},
			{ label: '非单独检测项目',name: 'is_sg_check', 				full:true, type:'checkbox' },
			{ label: '是否急诊',		name: 'is_emer_call', type:'checkbox', onValue:({ value, formData }) => {
				$fn.getRow(formData,'emer_charges').disabled = +value === 0
				this.setState({ formData })
			} },
			{ label: '急诊收费',		name: 'emer_charges' },
		],
	}
	
	componentDidMount(){
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 2000, 16000, 40000, 19000 ] })
			// 标本类型
			$fn.setRow( formData, 'spec_type', dic['2000'])
			// 检测方法
			$fn.setRow( formData, 'check_method', dic['16000'])
			// 检测系统/项目分组
			$fn.setRow( formData, 'kind_group', dic['40000'])
			// 项目属性
			$fn.setRow( formData, 'kind_type', dic['19000'])
			// 外包单位
			const _data = await f.getCache({ api:'getOutsourcingDepartList' })
			$fn.setRow( formData, 'outsourcing_company_code', _data.outsourcing_company )
			// 是否外包
			$fn.setRow( formData, 'is_outsourcing',  await f.bool_status )
			// 系统分类
			const sys_data = await f.getCache({ api:'conf-system-category/select', param:{ source_come: 'test_kind' }, refresh:true })
			$fn.setRow( formData, 'sys_category', sys_data )
			//
			this.setState({ formData })
		})
	}
	
	onSubmit = () => this.formRef.submit()
	
	// 获取外包数据
	onOut = async value => {
		const { formData } = this.state
		await $cache.then(async f=>{
			const type = +value === 1 ? 2 : 1
			// 岗位
			$fn.setRow( formData, 'project_id',  await f.getCache({ api:'project-team/laboratorySelect4Outsourcing', param:{type}, refresh:true }) )
		})
	}
	
	fetch = () => {
		const { formData } = this.state
		this.props.fetch( data => {
			// 单一项目设置时是否外包默认否 耿琰 20220630
			if(data.is_outsourcing===''){data.is_outsourcing='0'}
			this.formRef.setValue(data)
			const { is_emer_call, uuid, total_price, is_outsourcing } = data
			this.submit = { uuid, total_price}
			
			$fn.disabledRows(formData,'emer_charges', +is_emer_call === 0)
			$fn.disabledRows(formData,'outsourcing_company_code', +is_outsourcing === 0)
			
			this.onOut(is_outsourcing)
			
			this.setState({ price:total_price, formData })
		})
	}
	
	render(){
		const { edit } = this.props
		return (
			<>
				<Box title='项目基本信息' contentClassName='pt10 pr10' first>
					{ SubmitForm.call(this,{ msg:'保存', labelWidth:'s8', disabled:edit, api:'kind-info/edit', onForm: this.fetch, onOk:this.props.onAudit }) }
				</Box>
				{/* 新增字典 */}
				<AddDicModal onRef={ref => this.dicRef = ref} top={this} disabled={edit}/>
				{/* 从属项目 */}
				<DependenceProjectTable {...this.props} disabled={edit}/>
				{/* 其它项目 */}
				<OtherProjectTable {...this.props} disabled={edit}/>
				{/* 项目别名 */}
				<ProjectAliasTable {...this.props} disabled={edit}/>
				{/* 物价关联 */}
				<PriceLinkTable {...this.props}  price={this.state.price} disabled={edit}/>
			</>
		)
	}
}

export default Index